package com.zrspring.libv2.base;

/*   
 * Copyright (c) 2014-2014 China.ChongQing.MaiQuan Ltd All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


import com.zhy.http.okhttp.OkHttpUtils;
import com.zrspring.libv2.R;
import com.zrspring.libv2.util.LogUtil;

import butterknife.ButterKnife;

/**
 *
 * @author 请填写作者名
 * @ClassName: BaseActivity
 * @Description: 自定义activity类， 执行顺序 onCreate() > iniLayout() > iniView() >
 * findExtraData() > loadData(); onDestroy() > cleanMemory()
 * @date 2014-5-25 下午5:45:10
 * @ConputerUserName zrspring 2017年测试 2 3 4
 *
 */

public abstract class BaseActivity extends AppCompatActivity {

    /**
     * @Fields progressDialog :数据加载进度条
     */
    private ProgressDialog progressDialog;
    private Dialog networkConnectErrorDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isSetStatusBarTransparent()) {
            setStatusBarState();
        }

        setContentView(iniLayout());
        ButterKnife.bind(this);
        iniView(savedInstanceState);
        findExtraData(getIntent());
        loadData();
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeProgressDialog();
        cleanMemory();
        closeRetryDialog();
        ActivityCollector.removeActivity(this);
        ButterKnife.unbind(this);
    }

    public void setStatusBarState() {
//      //如果   4.3< 平台版本 < 5.0 打开4.4的透明
//		if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2 &&
//			Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ) {
//			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//		}
        //如果  平台版本 > 4.3 打开透明
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    /**
     * @return
     * @Title: isSetStatusBarTransparent
     * @Description: true 是否开启状态栏透明，适用于4.4-6.0，全部同一
     * false是使用Android 5.0,6.0的Desgin风格，对应style主题下的参数colorPrimaryDark
     */
    public abstract boolean isSetStatusBarTransparent();

    /**
     * @Title: iniLayout @Description: 设置布局文件 @return @throws
     */
    public abstract int iniLayout();

    /**
     * @Title: iniView @Description: 初始化界面，和配置熟悉，并添加监听等 @throws
     */
    public abstract void iniView(Bundle savedInstanceState);

    /**
     * @Title: findExtraData @Description: 获取intent里面的数据 @throws
     */
    public abstract void findExtraData(Intent data);

    /**
     * @Title: loadData @Description: 加载数据，调用服务器数据，或者数据库等 @throws
     */
    public abstract void loadData();

    /**
     * @Title: cleanMemory @Description: 清空内存工作 @throws
     */
    public abstract void cleanMemory();

    protected void showToastInDevelopment() {
        Toast.makeText(this, "In Development ...", Toast.LENGTH_SHORT).show();
    }

    /**
     * @Title: openProgressDialog @Description: 打开进度条 @param title @param
     * desc @throws
     */
    protected void openProgressDialog(String desc) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setMessage(desc);
            progressDialog.setCancelable(true);
            progressDialog.show();
        } else {
            progressDialog.show();
        }
    }

    protected void openProgressDialog(int messageResId) {
        String message = getResources().getString(messageResId);
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setMessage(message);
            progressDialog.setCancelable(true);
            progressDialog.show();
        } else {
            progressDialog.show();
        }
    }

    /**
     * @Title: closeProgressDialog @Description: 关闭进度条 @throws
     */
    protected void closeProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    protected void setProgressMessage(String message) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.setMessage(message);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
        //activity 销毁的时候不保存 Fragment状态
        super.onSaveInstanceState(outState);
    }

    public void openRetryDialog(
            String msg,
            boolean canCelable,
            DialogInterface.OnClickListener retryListener,
            DialogInterface.OnClickListener exitListener) {
        if (networkConnectErrorDialog == null) {
            networkConnectErrorDialog = new AlertDialog.Builder(this).
                    setMessage(msg).
                    setPositiveButton(getResources().getString(R.string.lib_no_net_retry), retryListener).
                    setNegativeButton(getResources().getString(R.string.lib_no_net_exit), exitListener).create();
        }
        networkConnectErrorDialog.setCancelable(canCelable);
        networkConnectErrorDialog.show();
    }

    public void closeRetryDialog() {
        if (networkConnectErrorDialog != null) {
            networkConnectErrorDialog.dismiss();
            networkConnectErrorDialog = null;
        }
    }
}
